package com.maimao.core.utils;

import com.google.common.collect.Lists;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtils {
    public static final String DEFAULT_PATTERN = "yyyy-MM-dd hh:mm:ss";

    public static String nowStr() {

        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
    }

    public static String todayStr() {

        return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }


    public static LocalDateTime now() {
        return LocalDateTime.now();
    }

    public static Date nowToDate() {
        return new Date();
    }

    public static String format(LocalDateTime time) {
        return format(time, DEFAULT_PATTERN);
    }

    public static String format(LocalDateTime time, String pattern) {
        return DateTimeFormatter.ofPattern(DEFAULT_PATTERN).format(time);
    }

    public static long diffHours(LocalDateTime start, LocalDateTime end) {
        return Duration.between(start, end).toHours();
    }

    public static long diffDaysByHours(LocalDateTime start, LocalDateTime end) {
        return Duration.between(start, end).toHours() / 24;
    }

    public static long diffDays(LocalDateTime start, LocalDateTime end) {
        return Duration.between(start, end).toDays();
    }

    public static Integer[] getYearMonthDay(Integer plusDay) {
        LocalDate now = LocalDate.now().plusDays(plusDay);
        int year = now.getYear();
        int month = now.getMonth().getValue();
        int day = now.getDayOfMonth();
        return new Integer[]{year, month, day};
    }

    /**
     * 第二天0点
     *
     * @return
     */
    public static LocalDateTime nextDayBegin() {
        return now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    public static void main(String[] args) {
   /*     ArrayList<String> strings = Lists.newArrayList(Arrays.asList("1", "2", "3", "4"));
        System.out.println(strings);
        List<String> list = strings.subList(2, strings.size());
        System.out.println(list);
        List<String> list1 = strings.subList(0, 2);
        System.out.println(list1);*/
       /* BigDecimal bigDecimal = new BigDecimal(11);
        BigDecimal divide = bigDecimal.divide(BigDecimal.valueOf(12), 2, RoundingMode.HALF_UP);
        System.out.println(divide.longValue());
        System.out.println(divide.intValue());
        System.out.println(divide.doubleValue());*/
        System.out.println(1 + "%");
        List<String> currentMonthDayStr = getCurrentMonthDayStr();
        System.out.println(currentMonthDayStr);
    }

    /**
     * 获取当月第一天
     *
     * @return
     */
    public static LocalDateTime firstDayOfMonth() {

        return firstDayOfMonth(null);
    }

    public static LocalDateTime firstDayOfMonth(Integer plusMonth) {
        int year = LocalDateTime.now().getYear();
        int month = LocalDateTime.now().getMonthValue();
        LocalDateTime currentFirstDay = LocalDateTime.of(year, month, 1, 0, 0, 0);
        if (plusMonth != null) {
            return currentFirstDay.plusMonths(plusMonth);
        }
        return currentFirstDay;
    }

    public static LocalDateTime firstDayOfYear() {
        return firstDayOfYear(null);
    }

    public static LocalDateTime firstDayOfYear(Integer plusYear) {
        int year = LocalDateTime.now().getYear();
        LocalDateTime currentFirstYear = LocalDateTime.of(year, 1, 1, 0, 0, 0);
        if (plusYear != null) {
            return currentFirstYear.plusYears(plusYear);
        }
        return currentFirstYear;
    }

    public static LocalDateTime lastDayOfYear() {
        int year = LocalDateTime.now().getYear();
        LocalDateTime currentLastDayOfYear = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        return currentLastDayOfYear;
    }


    public static int getToTalDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当月的日期str
     *
     * @return
     */
    public static List<String> getCurrentMonthDayStr() {
        int toTalDayOfMonth = getToTalDayOfMonth(new Date());
        List<String> res = Lists.newArrayListWithCapacity(toTalDayOfMonth);
        for (int i = 1; i <= toTalDayOfMonth; i++) {
            res.add(i + "日");
        }
        return res;

    }

    public static List<String> get12MonthStr() {
        List<String> res = Lists.newArrayListWithCapacity(12);
        for (int i = 1; i <= 12; i++) {
            res.add(i + "月");
        }
        return res;
    }

    public static List<String> getPreYearStr(int preYear) {
        List<String> res = Lists.newArrayListWithCapacity(12);
        for (int i = preYear; i <= LocalDateTime.now().getYear(); i++) {
            res.add(i + "年");
        }
        return res;
    }
}
